package com.geostar.constructionland.analyse.service.handler.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultCszbyjjbnt;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultZrbhq;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.analyseresult.CszbyjjbntService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author chengan
 * @description 城市周边永久基本农田
 */

@Component
@Slf4j
public class CszbyjjbntHandler implements IResultHandler{

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    CszbyjjbntService cszbyjjbntService;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        log.info("处理城市基本农田永久基本分析,状态码:{}", httpResult.getCode());
        HandlerResult result = HandlerResult.createBySuccessMessage("操作无误，恭喜成功!");
        if (HttpStatus.SC_OK == httpResult.getCode()) {
            /**解析xml格式,获取JSON数据**/
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(), AlyResult.class);
            log.info("body:" + httpResult.getBody());
            if (HttpStatus.SC_OK == alyResult.getStatus()) {
                List<AlyResultCszbyjjbnt> alyResultCszbyjjbnts = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultCszbyjjbnt>>() {
                        });
                /****如果不为空，存入数据库****/
                if(CollectionUtils.isNotEmpty(alyResultCszbyjjbnts)) {
                    alyResultCszbyjjbnts.forEach(alyResultCszbyjjbnt -> {
                        alyResultCszbyjjbnt.setFxbs(analyseModel.getAlyId());
                        /**四舍五入存表**/
                        alyResultCszbyjjbnt.setZymj(BigDecimal.valueOf(alyResultCszbyjjbnt.getZymjys()).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue());
                    });
                    cszbyjjbntService.saveBatch(alyResultCszbyjjbnts);
                }
            }else{
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
